<?php
//
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'get_detail';
if($_RQ['op']=='get_detail'){
	$id=intval($_RQ['id']);
	$return=model('jy_weishop/goods_item')->get_goodsitem($return);
	$return['options']['zu_data']=json_decode($return['options']['zu_data'],true);
	foreach($return['options']['zu_data'] as &$zu){
		$zu['title']=$zu['num'].$this->zuday_types[$zu['numtype']];
	}
	//$return['goods']['api_data']=json_decode($return['goods']['api_data'],true);
	//$return['goods']['price']=$return['goods']['api_data']['mi_price']?:$return['goods']['price'];
	//小程序二维码：
	$url='/jy_mfuwu/app/goods/detail/index?id='.$_SESSION['goodsid'].'&fromuser='.$_SESSION['uid'].'&merchid='.$_SESSION['merchid'];
	$pid='wxappnew'.$_SESSION['wxapp_id'].'_'.$_SESSION['goodsid'].'_'.$_SESSION['uid'];
	if($_SESSION['fromh5wxapp']){
			$h5url='jy_mfuwu/app.goods.detail//id='.$_SESSION['goodsid'].'&fromuser='.$_SESSION['uid'].'&merchid='.$_SESSION['merchid'];
			$url='/pages/index/index?url='.urlencode($h5url);
	}
	$return['goods']['appcode'] = model('jy_weishop/model')->get_wxappewm($url,$pid);
	//海报文件
	if($return['goods']['isposter']){
		if($_SESSION['fromh5wxapp'] || $_SESSION['container']=='wxapp'){
			$poster=model('jy_poster/model')->get_active(3,'',$return['goods']['id'],$_SESSION['openid'],$return['goods']['appcode'],'','jy_mfuwu');
		}else{
			$poster=model('jy_poster/model')->get_active(3,'',$return['goods']['id'],$_SESSION['openid'],'','','jy_mfuwu');
		}
	}
	$return['show1']=true;
	$return['posterimg']=$poster['poster'];
	$return['member']=model('core/member')->detail($_SESSION['uid'],array('mobile','avatar','nickname'));
	$return['sharedata']['link']=custom_url('jy_mfuwu/app.goods.detail//id='.$id.'&fromuser='.$_SESSION['uid'].'&current_plugin='.$_SESSION['current_plugin'].'&merchid='.$_SESSION['merchid'],true);
	set_html('title',$goods['title']?:'项目详情');
	exi($return);
}

if($_RQ['op']=='addtocart'){
	if(!$_SESSION['uid']){
		model('jy_weishop/member')->checklogin();
	}
	$post=get_postdata();
	$id=intval($post['goodsid']);
	$number=intval($post['num']);
	$isjson=intval($post['isjson']);
	if(!$id){
		exi('选择的项目不存在，请重新选择！','error');
	}
	model('jy_weishop/activity/sales')->check_sale_new(array(array('goodsid'=>$id)));
	if(!$number){
		$number=1;
	}
	$spsids = '';
	if($post['specid']){
		foreach($post['specid'] as $k=>&$s){
			if($k==0){
				$spsids .= $s;
			}else{
				$spsids .= '_'.$s;
			}
		}
	}
	$optionid = pdo_getcolumn('jy_weishop_goods_option',array('goodsid'=>$id,'specs'=>$spsids),'id');
	$goods=pdo_get('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	$ifgoods=pdo_get('jy_weishop_member_cart',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'goodsid'=>$id,'optionid'=>$optionid));
	$data=array(
		'uniacid'=>$_SESSION['uniacid'],
		'mid'=>$_SESSION['uid'],
		'goodsid'=>$id,
		'price'=>$goods['price'],
		'optionid'=>$optionid,
	);
	if($ifgoods){
		$data['total']=$ifgoods['total']+$number;
		model('jy_weishop/goods')->check_buy($data['goodsid'],intval($data['total']),intval($data['optionid']),true,true);
		pdo_update('jy_weishop_member_cart',$data,array('uniacid'=>$_SESSION['uniacid'],'id'=>$ifgoods['id']));
	}else{
		$data['total']=$number;
		model('jy_weishop/goods')->check_buy($data['goodsid'],intval($data['total']),intval($data['optionid']),true,true);
		//如果来自其他商户分销（保存商户ID）
		$data['merchid']=$goods['merchid']?:$this->session('frommerchid');
		pdo_insert('jy_weishop_member_cart',$data);
	}
	if($isjson){
		exi('success');
	}else{
		exi('成功加入购物车！');
	}
}
if($_RQ['op']=='get_optionsid'){
	$post=get_postdata();
	$id=$post['goodsid']?:$_SESSION['goodsid'];
	$store_id=$this->session('store_id');
	if(!$id){
		exi('请先选择项目！','error');
	}
	if($post['storeid']){
		
	}
	//规格
	$specids=explode('_',$post['specid']);
	
	//已选规格
	$selespename = '';
	if($specids){
			foreach($specids as $k=>$sp){
				$spec_item=pdo_get('jy_weishop_goods_spec_item',array('id'=>$sp,'uniacid'=>$_SESSION['uniacid']),array('id','title'));
				$selespename .=$spec_item['title'].'，';
			}
	}
	$optionsid=pdo_getcolumn('jy_weishop_goods_option',array('goodsid'=>$id,'specs'=>$post['specid']),'id');
	//默认项目规格价格：
	$goods=pdo_get('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	$goods['optionid']=$optionsid;
	$goods['storeid']=$store_id;
	$goods=model('jy_weishop/activity')->get_upgoods($goods);
	$goods['options']['zu_data']=json_decode($goods['options']['zu_data'],true);
	foreach($goods['options']['zu_data'] as &$zu){
		$zu['title']=$zu['num'].$this->zuday_types[$zu['numtype']];
	}
	$return['options']=$goods['options'];
	$return['options']['thumb']=$return['options']['thumb']?tomedia($return['options']['thumb']):tomedia($goods['image']);
	if($post['isdish']){
		$return['carttotal']=pdo_getcolumn('jy_weishop_member_cart',array('selected'=>1,'optionid'=>$return['options']['id'],'goodsid'=>$id,'mid'=> $_SESSION['uid'], 'uniacid' => $_SESSION['uniacid']),'total');
	}else{
		$return['carttotal']=pdo_getcolumn('jy_weishop_member_cart',array('optionid'=>$return['options']['id'],'goodsid'=>$id,'mid'=> $_SESSION['uid'], 'uniacid' => $_SESSION['uniacid']),'total');
	}
	$return['carttotal']=$return['carttotal']?:0;
	$return['selespename'] = $selespename;
	exi($return);
}
if($_RQ['op']=='change_address'){
	$post=get_postdata();
	$addressid = intval($post['addressid']);
	$id=$_SESSION['goodsid'];
	if(!$id){
		exi('请先选择项目！','error');
	}
	$address =  pdo_get('core_address',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid'],'id'=>$addressid));
	$address['pcd']=json_decode($address['pcd'],true);
	$return['address'] =$address;
	exi($return);
}
if($_RQ['op']=='like'){
	if(!$_SESSION['uid']){
		model('jy_weishop/member')->checklogin();
	}
	$id = $_SESSION['goodsid'];
	if(!$id){
		$post=get_postdata();
	}
	$like=pdo_get('jy_weishop_member_like',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'goodsid'=>$id),'id');
	if($like){
			pdo_delete('jy_weishop_member_like',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'goodsid'=>$id));
			exi('取消成功');
	}else{
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'goodsid'=>$id,
			'createtime'=>TIMESTAMP
		);
		pdo_insert('jy_weishop_member_like',$insert);
		exi('关注成功');
	}	
}
if($_RQ['op']=='check_buy'){
	$goodsid=$_RQ['goodsid']?intval($_RQ['goodsid']):$_SESSION['goodsid'];
	if(!$goodsid){
		exi('请选择项目！','error');
	}
	$goods=pdo_get('jy_weishop_goods',array('id'=>$goodsid),array('id','title','no_onebuy'));
	if($goods['no_onebuy']){
		exi('该项目，不能单独购买！','error');
	}
	model('jy_weishop/activity/sales')->check_sale_new(array(array('goodsid'=>$goodsid)));
	model('jy_weishop/goods')->check_buy($goodsid,$_RQ['num'],$_RQ['optionsid']);
	exi('','','jy_mfuwu/app.order.create//goodsid='.$goodsid.'&num='.$_RQ['num'].'&optionsid='.$_RQ['optionsid'].'&addressid='.$_RQ['addressid'].'&storeid='.$_RQ['storeid'].'&roomid='.$_RQ['roomid']);
}
if($_RQ['op']=='getcomments'){
	$goodsid=$_RQ['goodsid']?intval($_RQ['goodsid']):$_SESSION['goodsid'];
	$return['list']=model('jy_weishop/goods')->get_comments($goodsid,6,$comment_total,$_RQ['type'],$_RQ['page']);
	exi($return);
}
if($_RQ['op']=='getactive'){
	$goods_id=intval($_RQ['goods_id']);
	$active_id=intval($_RQ['active_id']);
	$active_plugin=$_RQ['active_plugin'];
	if($active_plugin && $goods_id){
		$active=model('jy_weishop/activity')->get_active($goods_id,$active_id,$active_plugin);
	}
	$return['active']=$active;
	exi($return);
}